﻿/*
=======================================================
    CheckWirerope【在用绳检测】
=======================================================
*/

namespace Services
{
    public interface ICheckWireropeRepository : IRepository<CheckWirerope>
    {
        CheckWirerope GetLastCheckWirerope(int usingWireropeID);
        List<CheckWirerope> GetAllCheckWirerope(int usingWireropeID);
        int InsertForAdmin(CheckWirerope checkWirerope, List<CheckWireropeDetail> checkWireropeDetails, UsingWirerope usingWirerope);
        void UpdateForAdmin(CheckWirerope checkWirerope, UsingWirerope usingWirerope);
        void Check(CheckWirerope checkWirerope, UsingWirerope usingWirerope);
        CheckWirerope GetPrevCheckWirerope(int usingWireropeID, DateTime? prevCreateDate);
    }


    public class CheckWireropeRepository : BaseRepository<CheckWirerope>, ICheckWireropeRepository
    {
        public CheckWireropeRepository(ISqlSugarClient dB, ICurrentUser currentUser, IQuery<CheckWirerope> query)
            : base(dB, currentUser, query)
        {
        }

        #region 获取一套绳子的最后一次的检测记录
        public CheckWirerope GetLastCheckWirerope(int usingWireropeID)
        {
            return _query.GetQueryable().Where(x => x.UsingWireropeID == usingWireropeID && x.Status == (int)CheckWirerope_Status.已检测 && x.SubmitDate != null)
                .OrderBy(x => x.CheckDate, OrderByType.Desc)
                .OrderBy(x => x.SubmitDate, OrderByType.Desc)
                .First();
        }
        #endregion

        #region 获取上次检测数据
        public CheckWirerope GetPrevCheckWirerope(int usingWireropeID, DateTime? prevCreateDate)
        {
            var where = Expressionable.Create<CheckWirerope>();
            where = where.And(x => x.UsingWireropeID == usingWireropeID && x.Status == (int)CheckWirerope_Status.已检测 && x.SubmitDate != null);
            if (prevCreateDate.HasValue)
            {
                where = where.And(x => x.CreateDate < prevCreateDate.Value);
            }

            return _query.GetQueryable().Where(where.ToExpression())
                   .OrderBy(x => x.CheckDate, OrderByType.Desc)
                  .OrderBy(x => x.SubmitDate, OrderByType.Desc)
                  .First();
        } 
        #endregion

        #region 获取一套绳子的所有完成的检测记录
        public List<CheckWirerope> GetAllCheckWirerope(int usingWireropeID)
        {
            return _query.GetQueryable().Where(x => x.UsingWireropeID == usingWireropeID && x.Status == (int)CheckWirerope_Status.已检测).ToList();
        }
        #endregion

        #region InsertForAdmin
        public int InsertForAdmin(CheckWirerope checkWirerope, List<CheckWireropeDetail> checkWireropeDetails, UsingWirerope usingWirerope)
        {
            var id = 0;
            id = Insert(checkWirerope);
            checkWirerope.CheckWireropeID = id;

            foreach (var detail in checkWireropeDetails)
            {
                detail.CheckWireropeID = id;
                detail.GroupInfoID = checkWirerope.GroupInfoID;

                var idTemp = _DB.Insertable(detail).ExecuteReturnIdentity();

                if (detail.CheckWireropeDetailDatas != null)
                {
                    foreach (var jtem in detail.CheckWireropeDetailDatas)
                    {
                        jtem.CheckWireropeID = checkWirerope.CheckWireropeID;
                        jtem.GroupInfoID = checkWirerope.GroupInfoID;
                        jtem.CheckWireropeID = checkWirerope.CheckWireropeID;
                        jtem.CheckWireropeDetailID = idTemp;

                        _DB.Insertable(jtem).ExecuteCommand();
                    }
                }

                if (detail.CheckWireropeDetailPositions != null)
                {
                    foreach (var mtem in detail.CheckWireropeDetailPositions)
                    {
                        mtem.CheckWireropeID = checkWirerope.CheckWireropeID;
                        mtem.GroupInfoID = checkWirerope.GroupInfoID;
                        mtem.CheckWireropeID = checkWirerope.CheckWireropeID;
                        mtem.CheckWireropeDetailID = idTemp;

                        _DB.Insertable(mtem).ExecuteCommand();
                    }
                }
            }

            if (usingWirerope != null)
            {
                _DB.Updateable(usingWirerope).ExecuteCommand();
            }
            return id;
        }
        #endregion

        #region UpdateForAdmin
        public void UpdateForAdmin(CheckWirerope checkWirerope, UsingWirerope usingWirerope)
        {
            _DB.Deleteable<CheckWireropeDetailPosition>().Where(x => x.CheckWireropeID == checkWirerope.CheckWireropeID).ExecuteCommand();
            _DB.Deleteable<CheckWireropeDetailData>().Where(x => x.CheckWireropeID == checkWirerope.CheckWireropeID).ExecuteCommand();
            _DB.Deleteable<CheckWireropeDetail>().Where(x => x.CheckWireropeID == checkWirerope.CheckWireropeID).ExecuteCommand();

            foreach (var item in checkWirerope.CheckWireropeDetails)
            {
                item.CheckWireropeID = checkWirerope.CheckWireropeID;
                item.GroupInfoID = checkWirerope.GroupInfoID;
                var id = _DB.Insertable(item).ExecuteReturnIdentity();

                if (item.CheckWireropeDetailDatas != null)
                {
                    foreach (var jtem in item.CheckWireropeDetailDatas)
                    {
                        jtem.CheckWireropeID = checkWirerope.CheckWireropeID;
                        jtem.GroupInfoID = checkWirerope.GroupInfoID;
                        jtem.CheckWireropeDetailID = id;

                        _DB.Insertable(jtem).ExecuteCommand();
                    }
                }

                if (item.CheckWireropeDetailPositions != null)
                {
                    foreach (var mtem in item.CheckWireropeDetailPositions)
                    {
                        mtem.CheckWireropeID = checkWirerope.CheckWireropeID;
                        mtem.GroupInfoID = checkWirerope.GroupInfoID;
                        mtem.CheckWireropeDetailID = id;

                        _DB.Insertable(mtem).ExecuteCommand();
                    }
                }
            }

            if (usingWirerope != null)
            {
                _DB.Updateable(usingWirerope).ExecuteCommand();
            }

            Update(checkWirerope);
        }
        #endregion

        #region Check
        public void Check(CheckWirerope checkWirerope, UsingWirerope usingWirerope)
        {
            _DB.Deleteable<CheckWireropeDetailPosition>().Where(x => x.CheckWireropeID == checkWirerope.CheckWireropeID).ExecuteCommand();
            _DB.Deleteable<CheckWireropeDetailData>().Where(x => x.CheckWireropeID == checkWirerope.CheckWireropeID).ExecuteCommand();
            _DB.Deleteable<CheckWireropeDetail>().Where(x => x.CheckWireropeID == checkWirerope.CheckWireropeID).ExecuteCommand();

            foreach (var item in checkWirerope.CheckWireropeDetails)
            {
                var id = _DB.Insertable(item).ExecuteReturnIdentity();

                if (item.CheckWireropeDetailDatas != null)
                {
                    foreach (var jtem in item.CheckWireropeDetailDatas)
                    {
                        jtem.CheckWireropeID = checkWirerope.CheckWireropeID;
                        jtem.GroupInfoID = checkWirerope.GroupInfoID;
                        jtem.CheckWireropeDetailID = id;

                        _DB.Insertable(jtem).ExecuteCommand();
                    }
                }

                if (item.CheckWireropeDetailPositions != null)
                {
                    foreach (var mtem in item.CheckWireropeDetailPositions)
                    {
                        mtem.CheckWireropeID = checkWirerope.CheckWireropeID;
                        mtem.GroupInfoID = checkWirerope.GroupInfoID;
                        mtem.CheckWireropeDetailID = id;

                        _DB.Insertable(mtem).ExecuteCommand();
                    }
                }
            }

            if (usingWirerope != null)
            {
                _DB.Updateable(usingWirerope).ExecuteCommand();
            }

            Update(checkWirerope);
        }
        #endregion
    }
}
